package cn.neusoft.controller;


import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.pojo.CheckRequest;
import cn.neusoft.pojo.Cost;
import cn.neusoft.pojo.MedicalRecordDisease;
import cn.neusoft.pojo.Prescription;
import cn.neusoft.query.CostQuery;
import cn.neusoft.service.ICheckRequestService;
import cn.neusoft.service.ICostService;
import cn.neusoft.service.IPrescriptionService;
import cn.neusoft.vo.ResultBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.transform.Result;
import java.util.List;

/**
 * <p>
 *   费用接口
 * </p>
 *
 * @author hxj
 * @since 2024-12-25
 */
@RestController
@RequestMapping("/cost")
public class CostController extends BaseController<Cost, CostQuery>{

    @Autowired
    ICostService costService;

    @Autowired
    ICheckRequestService checkRequestService;

    @Autowired
    IPrescriptionService prescriptionService;

    @Transactional
    @PostMapping("/pay")
    public ResultBean pay(@RequestBody CostQuery costQuery){

        if (costQuery==null||
                StringUtils.isEmpty(costQuery.getPayMethod()) ||
                costQuery.getSettleCategoryId()==null)   {
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        boolean update = costService.update(costQuery.getUpdateWrapper());

        if(!update){
            throw new AppException(400005,"支付费用失败");
        }

        //可以通过费用的id查询对应的ref_id(关联的检查表的id值)
        //修改对应的申请表中的状态为已缴费   update check_request set check_state='已缴费' where id=?
        //遍历每一个费用的id
        costQuery.getIds().forEach(costId->{
            //根据费用id查询对应的费用对象
            Cost cost = costService.getOne(new QueryWrapper<Cost>().eq("id", costId));
            String costtype = cost.getCosttype();
            if ("检查".equals(costtype)){
                UpdateWrapper<CheckRequest> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("check_state","已缴费")
                        .eq("id",cost.getRefId());
                boolean up = checkRequestService.update(updateWrapper);
                if(!up){
                    throw new AppException(300003,"更新检查申请缴费状态失败");
                }
            }else if("药品".equals(costtype)){
                UpdateWrapper<Prescription> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("drug_state","已缴费")
                        .eq("id",cost.getRefId());
                boolean up = prescriptionService.update(updateWrapper);
                if(!up){
                    throw new AppException(300004,"更新药品缴费状态失败");
                }
            }
        });

        return ResultBean.SUCCESS();
    }

    @PostMapping("/conditions")
    public ResultBean getByConditions(@RequestBody CostQuery costQuery){
        QueryWrapper<Cost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("register_id",costQuery.getRegisterId());

        List<Cost> list = costService.list(queryWrapper);
        return ResultBean.SUCCESS(list);
    }

}
